<template>
	<!-- <a-card :bordered="false" > -->
		<!-- :alert="options.alert.show" -->
		<!-- <s-table ref="tableRef" :columns="columns" :data="loadData()" bordered :row-key="(record) => record.id"
			:tool-config="toolConfig" :row-selection="options.rowSelection"> -->
		<!--  v-if="hasPerm('bizHelpFamilyAdd')" -->
		<!-- v-if="hasPerm('bizHelpFamilyBatchDelete')" -->
		<a-space v-if="flag==0">
			<a-button type="primary" @click="formRef.onOpen()">
				<template #icon><plus-outlined /></template>
				新增
			</a-button>
			<!-- <xn-batch-delete :selectedRowKeys="selectedRowKeys" @batchDelete="deleteBatchBizHelpFamily" /> -->
		</a-space>
		<a-table bordered :dataSource="dataSource" :columns="columns">
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action' && flag == 0">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('bizHelpEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['bizHelpEdit', 'bizHelpDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteBizHelpFamily(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('bizHelpDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</a-table>
		<!-- </s-table> -->
	<!-- </a-card> -->
	<Form ref="formRef" @successful="addData" />
</template>

<script setup name="help">
import { cloneDeep } from 'lodash-es'
import Form from './form.vue'
import bizHelpFamilyApi from '@/api/biz/bizHelpFamilyApi'
const props = defineProps({
	dataSource: {
		type: Array,
		default: () => []
	},
	flag: {
		type: Number,
		default: 0
	},
});
const tableRef = ref()
const formRef = ref()
const toolConfig = { refresh: false, height: false, columnSetting: false, striped: false }
const emit = defineEmits(['getData']);

const columns = [
	{
		title: '名称',
		dataIndex: 'familyName'
	},
	{
		title: '性别',
		dataIndex: 'familyGender'
	},
	{
		title: '生日',
		dataIndex: 'familyBirthday'
	},
	{
		title: '身份证号',
		dataIndex: 'familyId'
	},
	{
		title: '健康情况',
		dataIndex: 'familyHealth'
	},
	{
		title: '单位/学校',
		dataIndex: 'familyOrg'
	},
	// {
	// 	title: '关联申请表',
	// 	dataIndex: 'heipId'
	// },
]
// 操作栏通过权限判断是否显示
if (hasPerm(['bizHelpEdit', 'bizHelpDelete'])&& props.flag==0) {
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: 150
	})
}
const selectedRowKeys = ref([])
// 列表选择配置
const options = {
	// columns数字类型字段加入 needTotal: true 可以勾选自动算账
	alert: {
		show: true,
		clear: () => {
			selectedRowKeys.value = ref([])
		}
	},
	rowSelection: {
		onChange: (selectedRowKey, selectedRows) => {
			selectedRowKeys.value = selectedRowKey
		}
	}
}
const loadData = (parameter) => {
	return props.dataSource
}

const getData = (parameter) => {
	emit('getData', props.dataSource);
	return props.dataSource
}
const addData = (data) => {

	if (data.id) {
		let temp = props.dataSource.findIndex(item => {
			return item.id == data.id
		})
		props.dataSource[temp] = Object.assign({}, data)
	} else {
		nextTick(() => {
			data.id = new Date().getTime() + 'helpFamily'
			props.dataSource.push(data)
		})

	}
}
// 添加家庭成员信息

// 重置
const reset = () => {
	searchFormRef.value.resetFields()
	tableRef.value.refresh(true)
}
// 删除
const deleteBizHelpFamily = (record) => {
	let params = [
		{
			id: record.id
		}
	]
	let temp = props.dataSource.findIndex(item => {
		return item.id == record.id
	})
	props.dataSource.splice(temp, 1)
}
// 批量删除
const deleteBatchBizHelpFamily = (params) => {
	bizHelpFamilyApi.bizHelpFamilyDelete(params).then(() => {
		tableRef.value.clearRefreshSelected()
	})
}
defineExpose({
	getData
})

</script>
